import React, {PropsWithChildren, useEffect, useState} from 'react'
import styles from "./EditHomeApplianceForm.module.sass"
import {Category, digitalBrands, getDigitCategories, homeApplianceBrands} from "../apis/OrderApi";
import {
    Button,
    Divider,
    Form,
    Input
} from "@nutui/nutui-react-taro";
import Taro from "@tarojs/taro";
import {OffsetDateTime} from "@js-joda/core";
import IDCardUploadArea from "./IDCardUploadArea";
import qiniuUploadStore from "../store/QiniuUploadStore";
import InvoiceUploadArea from "./InvoiceUploadArea";
import QiniuImageUploader from "./QiniuImageUploader";
import PaymentFormItems from "./PaymentFormItems";
import OrderGoodsInfoFormItems from "./OrderGoodsInfoFormItems";
import BuyerFormItems from "./BuyerFormItems";

export interface HomeApplianceInitialValue {
    name: string;
    idCard?: string;
    phoneNumber?: string;
    paymentFee?: number;
    governmentSubsidies?: number;
    category: Category;  // 假设 XinyuanOrder.Category 映射为 string
    brand: string
    paymentDateTime?: OffsetDateTime;
    address?: string
    imei1?: string
    imei2?: string
    unionPayOrderNumber?: string;
    invoiceNumber?: string;
    barcode?: string;
    serialNumber?: string;
    model?: string;
    idCardFrontPicture?: string;
    idCardBackPicture?: string;
    productImagePictures: string[];
    invoicePicture?: string;
    unionPayInvoicePicture?: string;
    deliveryNotePicture?: string;
}

export interface EditHomeApplianceFormProps extends PropsWithChildren {
    initialValue?: HomeApplianceInitialValue
    onConfirm: (value: HomeApplianceInitialValue) => Promise<void>
}

const EditHomeApplianceForm: React.FC<EditHomeApplianceFormProps> = ({initialValue, onConfirm}) => {

    useEffect(() => {
        qiniuUploadStore.getToken()
    }, []);
    const [confirming, setConfirming] = useState(false)

    const [paymentDateTime, setPaymentDateTime] = useState<OffsetDateTime>()

    const [front, setFront] = useState<string>()
    const [back, setBack] = useState<string>()

    const [invoicePicture, setInvoicePicture] = useState<string>()
    const [unionPayInvoicePicture, setUnionPayInvoicePicture] = useState<string>()

    const [productImages, setProductImages] = useState<string[]>([])

    useEffect(() => {
        setFront(initialValue?.idCardFrontPicture)
        setBack(initialValue?.idCardBackPicture)
        setInvoicePicture(initialValue?.invoicePicture)
        setUnionPayInvoicePicture(initialValue?.unionPayInvoicePicture)
        setProductImages(initialValue?.productImagePictures ?? [])
        setPaymentDateTime(initialValue?.paymentDateTime)
    }, [initialValue]);

    return <>
        <Form key={JSON.stringify({name: initialValue?.name})}
              initialValues={initialValue ? {
                  ...initialValue,
                  category: initialValue.category ? [initialValue.category] : undefined,
                  brand: initialValue.brand ? [initialValue.brand] : undefined,

              } : undefined}
              footer={<Button formType={"submit"} type={"primary"}
                              loading={confirming} block>保存</Button>}
              className={styles.container}
              onFinish={async (values) => {
                  setConfirming(true)
                  try {
                      await onConfirm({
                          ...values,
                          paymentDateTime: paymentDateTime,
                          invoicePicture,
                          unionPayInvoicePicture,
                          idCardFrontPicture: front,
                          idCardBackPicture: back,
                          productImagePictures: productImages,
                          brand: values.brand ? values.brand[0] : undefined,
                          energyEfficiencyLevel: values.energyEfficiencyLevel ? values.energyEfficiencyLevel[0] : undefined,
                          category: values.category ? values.category[0] : undefined,
                      })
                      Taro.navigateBack()
                  } finally {
                      setConfirming(false)
                  }
              }}
        >
            <BuyerFormItems/>
            <Divider>商品信息</Divider>

            <OrderGoodsInfoFormItems categories={getDigitCategories()} brands={digitalBrands}/>

            <Form.Item label={"IMEI1"} name={"imei1"}>
                <Input/>
            </Form.Item>
            <Form.Item label={"IMEI2"} name={"imei2"}>
                <Input/>
            </Form.Item>
            <Divider>付款信息</Divider>
            <PaymentFormItems paymentDateTime={paymentDateTime} onPaymentDateTimeChange={setPaymentDateTime}/>

            <IDCardUploadArea onFrontChange={setFront} onBackChange={setBack} back={back} front={front}/>
            <InvoiceUploadArea invoicePicture={invoicePicture} onInvoicePictureChange={setInvoicePicture}
                               unionPayInvoicePicture={unionPayInvoicePicture}
                               onUnionPayInvoicePictureChange={setUnionPayInvoicePicture}/>
            <Divider>产品照片</Divider>
            <QiniuImageUploader multiple value={productImages} onChange={setProductImages}/>
        </Form>
    </>
}

export default EditHomeApplianceForm


